import type { AxiosResponse } from "axios";
import axios from "./index";
import type { Address, Result } from "./types";

export const createOrUpdateAddress = async (
  address: Address
): Promise<AxiosResponse<any>> => {
  let { data } = axios.post(`/address/createOrUpdate`, address);
  return data;
};
// 删除地址
export const deleteAddress = async (id: number): Promise<Result> => {
  let { data } = await axios.delete(`/address/${id}`);
  return data;
};

// 设置默认地址
export const setDefaultAddress = async (
  id: number,
  userId: number
): Promise<AxiosResponse<any>> => {
  try {
    const response = await axios.put(`/address/set-default`, {
      id,
      userId,
    });
    return response;
  } catch (error) {
    throw new Error(`Error in setDefaultAddress: ${error}`);
  }
};

// 获取所有地址
export const getAllAddresses = async (
  userId: number
): Promise<AxiosResponse<any>> => {
  try {
    const response = await axios.get(`/address/getAll/${userId}`);
    return response.data;
  } catch (error) {
    throw new Error(`Error in getAllAddresses: ${error}`);
  }
};
